#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "minunit.h"
#include "student_manager.h"

// 声明外部测试计数器
extern int tests_run;

// 测试管理器创建与销毁
char *test_manager_creation() {
    StudentManager *manager = manager_create(5);
    mu_assert("创建管理器失败", manager != NULL);
    mu_assert("初始容量错误", manager_get_count(manager) == 0);
    
    manager_destroy(manager);
    return NULL;
}

// 测试添加学生
char *test_manager_add() {
    StudentManager *manager = manager_create(2);
    Student *s1 = student_create("张三", 1, 90.0);
    Student *s2 = student_create("李四", 2, 85.0);
    
    mu_assert("添加学生1失败", manager_add_student(manager, s1) == 1);
    mu_assert("添加后数量错误", manager_get_count(manager) == 1);
    
    mu_assert("添加学生2失败", manager_add_student(manager, s2) == 1);
    mu_assert("添加后数量错误", manager_get_count(manager) == 2);
    
    // 测试自动扩容
    Student *s3 = student_create("王五", 3, 92.5);
    mu_assert("添加学生3（扩容）失败", manager_add_student(manager, s3) == 1);
    mu_assert("扩容后数量错误", manager_get_count(manager) == 3);
    
    manager_destroy(manager);
    return NULL;
}

// 测试查找学生
char *test_manager_find() {
    StudentManager *manager = manager_create(2);
    manager_add_student(manager, student_create("张三", 1, 90.0));
    manager_add_student(manager, student_create("李四", 2, 85.0));
    
    Student *found = manager_find_student(manager, 2);
    mu_assert("查找学生失败", found != NULL);
    mu_assert("查找结果错误", strcmp(student_get_name(found), "李四") == 0);
    
    found = manager_find_student(manager, 99);
    mu_assert("查找不存在学生应该返回NULL", found == NULL);
    
    manager_destroy(manager);
    return NULL;
}

// 测试删除学生
char *test_manager_remove() {
    StudentManager *manager = manager_create(2);
    manager_add_student(manager, student_create("张三", 1, 90.0));
    manager_add_student(manager, student_create("李四", 2, 85.0));
    
    mu_assert("删除学生失败", manager_remove_student(manager, 1) == 1);
    mu_assert("删除后数量错误", manager_get_count(manager) == 1);
    mu_assert("删除后仍能找到学生", manager_find_student(manager, 1) == NULL);
    
    mu_assert("删除不存在学生应该失败", manager_remove_student(manager, 99) == 0);
    
    manager_destroy(manager);
    return NULL;
}

// 测试更新学生
char *test_manager_update() {
    StudentManager *manager = manager_create(1);
    manager_add_student(manager, student_create("张三", 1, 90.0));
    
    mu_assert("更新学生失败", manager_update_student(manager, 1, "张三改", 95.0) == 1);
    Student *updated = manager_find_student(manager, 1);
    mu_assert("更新后姓名错误", strcmp(student_get_name(updated), "张三改") == 0);
    mu_assert("更新后分数错误", student_get_score(updated) == 95.0f);
    
    mu_assert("更新不存在学生应该失败", manager_update_student(manager, 99, "测试", 0) == 0);
    
    manager_destroy(manager);
    return NULL;
}

// 新增：测试导入导出功能
char *test_manager_import_export() {
    // 创建临时管理器
    StudentManager *manager = manager_create(5);
    manager_add_student(manager, student_create("张三", 1, 90.0));
    manager_add_student(manager, student_create("李四", 2, 85.0));

    // 测试导出
    int export_result = manager_export_to_file(manager, "test_export.txt");
    mu_assert("导出文件失败", export_result == 1);

    // 创建新管理器用于导入测试
    StudentManager *import_manager = manager_create(5);
    int imported = manager_import_from_file(import_manager, "test_export.txt");
    mu_assert("导入文件失败", imported == 2);
    mu_assert("导入后数量不匹配", manager_get_count(import_manager) == 2);

    // 验证导入的数据
    Student *imported_stu = manager_find_student(import_manager, 1);
    mu_assert("导入的数据不匹配（张三）", 
              imported_stu && strcmp(student_get_name(imported_stu), "张三") == 0);

    // 清理临时文件
    remove("test_export.txt");  // Linux/Mac，Windows可用DeleteFile

    manager_destroy(manager);
    manager_destroy(import_manager);
    return NULL;
}

// 管理器模块测试套件
char *test_manager_suite() {
    mu_run_test(test_manager_creation);
    mu_run_test(test_manager_add);
    mu_run_test(test_manager_find);
    mu_run_test(test_manager_remove);
    mu_run_test(test_manager_update);
    mu_run_test(test_manager_import_export);  // 新增测试
    return NULL;
}